import { Injectable } from '@angular/core';
import {
  Http,
  Response,
  URLSearchParams,
  Headers,
  RequestOptions,
  RequestOptionsArgs
} from '@angular/http';

import { Observable } from 'rxjs/Observable';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';

import { environment } from '../../../environments/environment';
import { HttpClient } from '@angular/common/http';

@Injectable()
export class UtilsService {
    private apiBaseUrl = environment.apiBaseUrl;

    constructor (private http: HttpClient) {}

    // http://47.100.161.202:8080/admin/channel/list
    // 文件上传接口
    postUploadFiles(param: object): Observable<object> {
        const file: File = param['file'];
        const formData: FormData = new FormData();
        formData.append('fileType', param['fileType']);
        formData.append('file', file);

        return this.http.post(this.apiBaseUrl + 'admin/file/upload', formData)
    }

    // 上传base64图片
    base64UpLoad(base64: any): Observable<object> {
        const param = {
            base64Data: base64
        };
        return this.http.post(this.apiBaseUrl + 'admin/file/base64UpLoad' , {param})
    }

    private extractData(res: Response) {
        const body = res.json();
        return body || { };
    }

    private handleError (error: Response | any) {
        return Observable.throw(error);
    }
}
